/**   
 * @Title: BeidanBuyLottery.java 
 * @Package com.esun.rabbit2.viewer.mainact.lotteryhallact.beidanapg 
 * @Description: 北京足球单场选中列表页面
 * @author:Kael gaojian@500wan.com   
 * @date: 2012-4-9 上午9:40:57
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact.beidanagp;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Hashtable;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnDismissListener;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout.LayoutParams;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.libc.ESInterface;
import com.esun.libc.TouZhuData;
import com.esun.model.UserInfo;
import com.esun.model.beijingdanchang.BeidanAgainstInfo;
import com.esun.model.beijingdanchang.BeidanLotteryInfo;
import com.esun.rabbit2.communicaters.beijingdanchang.BeiDanBuyCommunicater;
import com.esun.rabbit2.viewer.LoginAct;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.BeidanAgp;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryBuyLottery;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryChooseList;
import com.esun.rabbit2.viewer.mainact.mylotteryact.recharg.RechargeMenuPage;
import com.esun.ui.R;
import com.esun.util.LoginTimeControl;
import com.esun.util.ViewerUtil;
import com.esun.util.custom.GridViewNoScroll;
import com.esun.util.log.EDebug;

/**
 * @ClassName: BeidanBuyLottery
 * @Description: 北京足球单场选中列表页面
 * @author: Kael gaojian@500wan.com
 * @date: 2012-4-9 上午9:40:57
 * 
 */
public class BeidanBuyLottery extends SportteryBuyLottery implements OnClickListener {
	private int zhushu;// 组数
	private int beishu;// 倍数
	private int guoguanType; // 过关类型
	private String codes; // 请求参数
	private String danma; // 胆码
	private String sgtype;// 过关类型
	private String strGuoguan; // 过关的字符串
	private String expect;// 期号
	private String version;// 版本号
	private Context context;
	private View view;
	private LayoutInflater inflater;
	private SportteryChooseList chooseListPage;// 选择列表页

	// 界面控件
	private Button btnBack;// 后退
	private Button btnGuoguan;// 过关方式
	private Button btnClean;// 清空列表
	private Button btnHemai;// 发起合买
	private Button btnNormalBuy;// 立即购买

	private TextView tvGuoguan;// 过关方式
	private TextView tvChangCount;// 注数
	private TextView tvBetCount;// 倍数
	private TextView tvMoney;// 金额
	private TextView tvJackpot;// 理论最高奖金
	private TextView tvJiezhiTime;// 截止时间
	// private TextView tvBalance;//余额
	private EditText edtBeitou;// 倍数
	private Dialog dialogGuoguan;// 过关方式Dialog
	private GridView gridViewDuochuan;// 多串过关
	private GridView gridViewZiyou;// 自由过关
	private RadioButton rbZiyou;
	private DuochuanListAdapter duochuanListAdapter;// 对话框中多串过关GridView的适配器

	private ArrayList<CheckBox> listCbxDan = new ArrayList<CheckBox>();

	private ArrayList<Hashtable<String, Object>> listSGType = new ArrayList<Hashtable<String, Object>>();
	private ArrayList<Hashtable<String, Object>> listZiyou = new ArrayList<Hashtable<String, Object>>();
	private ArrayList<Hashtable<String, Object>> listDuochuan = new ArrayList<Hashtable<String, Object>>();
	private ArrayList<ArrayList<CheckBox>> danList = new ArrayList<ArrayList<CheckBox>>();

	private int limitDanForDialogGuoguan;// 已选中胆码数量

	private LinearLayout linearList;

	private BeiDanBuyCommunicater beiDanNormalBuyCommunicater;// 北单复式代购通信接口
	private ProgressDialog progressdialogNormalBuy;// 进度框
	// 多串过关上次选中的按钮index
	private int lastItem = -1;

	public BeidanBuyLottery(Context context, View view) {
		this.view = view;
		this.context = context;
		inflater = ((Activity) context).getLayoutInflater();
		init();
		handlerInit();
		updateListView();
	}

	/**
	 * 数据初始化
	 * 
	 * @throws NameNotFoundException
	 */
	private void dataInit() {
		zhushu = 0;
		beishu = 1;
		guoguanType = 0;
		codes = Config.NULLMARK;
		danma = Config.NULLMARK;
		sgtype = Config.NULLMARK;
		listSGType.clear();
		listZiyou.clear();
		listDuochuan.clear();
		strGuoguan = "过关方式";
		// 加载过关方式
		updateGuoguanTypeList();
		listSGType.addAll(listZiyou);
		EDebug.o("attaInit中listSGType1的大小为" + listSGType.size());
		if (!listSGType.isEmpty()) {
			guoguanType = 3;
			sgtype = Integer.toString((Integer) listSGType.get(listSGType.size() - 1).get(TAG));
			listSGType.get(listSGType.size() - 1).put(IS_CHECKED, true);
			strGuoguan = (String) listSGType.get(listSGType.size() - 1).get(STR_TYPE);
			EDebug.o("不为空时sgtype为" + sgtype);
			EDebug.o("不为空时strGuoguan为" + strGuoguan);
		}
		for (int i = 0; i < listCbxDan.size(); i++) {
			listCbxDan.get(i).setEnabled(false);
		}
		setLimitDanNum(7);// 胆码个数上限
		updateTextViewSet();// 更新TextView
	}

	/**
	 * @Title: setListCbxDan
	 * @Description: 设置胆码状态
	 * @param: @param b 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void setListCbxDan(boolean b) {
		for (int i = 0; i < listCbxDan.size(); i++) {
			listCbxDan.get(i).setEnabled(b);
		}
	};

	/** 过关方式在删除一条的时候修改操作 */
	private void updateGuoGuanType() {

		if (guoguanType == 3) {
			// 自由过关
			for (int i = listSGType.size() - 1; i > 0; i--) {
				if ((Boolean) listSGType.get(i).get(IS_CHECKED)) {
					EDebug.o(BeidanBuyLottery.this, "act is " + getActiveItemNum() + "& team count is " + (Integer) listSGType.get(i).get(TEAM_COUNT) + "++++++++++++++++");
					if (getActiveItemNum() < ((Integer) listSGType.get(i).get(TEAM_COUNT))) {
						listSGType.get(i).put(IS_CHECKED, false);
						EDebug.o(BeidanBuyLottery.this, "i is " + i + "& activ num is " + getActiveItemNum() + "+++++++++++++++++");
						break;
					} else if (getActiveItemNum() == ((Integer) listSGType.get(i).get(TEAM_COUNT))) {
						cleanDanItem();
						setListCbxDan(false);
					} else {
						setListCbxDan(true);
						setLimitDanNum((Integer) listSGType.get(i).get(TEAM_COUNT) - 1);
					}
				}
			}
			StringBuffer sb = new StringBuffer();
			StringBuffer sbSgType = new StringBuffer();
			for (int i = 0; i < listSGType.size(); i++) {
				if ((Boolean) listSGType.get(i).get(IS_CHECKED)) {
					sb.append((String) listSGType.get(i).get(STR_TYPE)).append(Config.ONESPACE);
					sbSgType.append(Integer.toString((Integer) listSGType.get(i).get(TAG))).append(Config.COMMA);
				}
			}
			strGuoguan = sb.toString();
			if (strGuoguan.length() == 0) {
				strGuoguan = "请选择过关方式";
				setListCbxDan(true);
				setLimitDanNum(7);
				sgtype = Config.NULLMARK;
				guoguanType = 0;
			} else {
				sgtype = sbSgType.toString();
				sgtype = sgtype.substring(0, sgtype.length() - 1);
			}
			EDebug.o(BeidanBuyLottery.this, "size is " + sgtype.length() + "&& valve is " + sgtype + "++++++++++++++++++++++++++++++");
			updateTextViewSet();
		} else if (guoguanType == 2) {
			// 多串过关
			int maxTeamCount = 0;
			for (int i = listSGType.size() - 1; i > 0; i--) {
				if ((Boolean) listSGType.get(i).get(IS_CHECKED)) {
					maxTeamCount = (Integer) listSGType.get(i).get(TEAM_COUNT);
					break;
				}
			}
			if (getActiveItemNum() < maxTeamCount) {
				listSGType.clear();
				updateGuoguanTypeList();
				listSGType.addAll(listZiyou);
				guoguanType = 0;
				sgtype = Config.NULLMARK;
				strGuoguan = "请选择过关方式";
				setListCbxDan(true);
				setLimitDanNum(7);
				updateTextViewSet();
			} else if (getActiveItemNum() == maxTeamCount) {
				cleanDanItem();
				setListCbxDan(false);
				updateTextViewSet();
			} else {
				setListCbxDan(true);
				setLimitDanNum(maxTeamCount - 1);
			}
		}
	}

	/** 更新主列表 */
	private void updateListView() {
		linearList.removeAllViews();
		listCbxDan.clear();
		danList.clear();
		LayoutParams param = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		ArrayList<Hashtable<String, Object>> beidanLotteryList = new ArrayList<Hashtable<String, Object>>();
		beidanLotteryList = BeidanLotteryInfo.getBeiDanLotteryInfo().getLotterList();
		for (int i = 0; i < beidanLotteryList.size(); i++) {
			final int i_ = i;
			if ((Boolean) beidanLotteryList.get(i).get(IS_ACTIVE)) {
				EDebug.o(BeidanBuyLottery.this, " 走你");
				BeidanAgainstInfo beiDanAgainstInfo = (BeidanAgainstInfo) beidanLotteryList.get(i).get(INFO);
				View listItem = inflater.inflate(R.layout.beidan_choose_act_list_item, null);
				// 期号
				expect = beiDanAgainstInfo.getExpect();
				// 版本号
				version = UserInfo.getUserInfo().getCurrentVersion();
				listItem.setTag(i);
				TextView tv_homeName = (TextView) listItem.findViewById(R.id.beidanchooseactlistitem_tv_homeName);// 主队
				TextView tv_rangqiu = (TextView) listItem.findViewById(R.id.beidanchooseactlistitem_tv_rangqiu);// 让球
				TextView tv_visiterName = (TextView) listItem.findViewById(R.id.beidanchooseactlistitem_tv_visiterName);// 客队
				tv_homeName.setText(strLengthOption((String) beiDanAgainstInfo.getHomeSxName(), 3));
				tv_rangqiu.setText((String) beiDanAgainstInfo.getRangqiu());
				tv_visiterName.setText(strLengthOption((String) beiDanAgainstInfo.getGuestSxName(), 3));
				final CheckBox cbxW = (CheckBox) listItem.findViewById(R.id.beidanchooseactlistitem_cbx_w);// 胜CheckBox
				final CheckBox cbxD = (CheckBox) listItem.findViewById(R.id.beidanchooseactlistitem_cbx_d);// 平CheckBox
				final CheckBox cbxL = (CheckBox) listItem.findViewById(R.id.beidanchooseactlistitem_cbx_l);// 负CheckBox
				final CheckBox cbxDan = (CheckBox) listItem.findViewById(R.id.beidanchooseactlistitem_cbx_dan);// 胆CheckBox
				final TextView tvWPL = (TextView) listItem.findViewById(R.id.beidanchooseactlistitem_tv_wpl);// 胜赔率TextView
				final TextView tvDPL = (TextView) listItem.findViewById(R.id.beidanchooseactlistitem_tv_dpl);// 平赔率TextView
				final TextView tvLPL = (TextView) listItem.findViewById(R.id.beidanchooseactlistitem_tv_lpl);// 负赔率TextView
				final TextView tvW = (TextView) listItem.findViewById(R.id.beidanchooseactlistitem_tv_w);// 胜TextView
				final TextView tvD = (TextView) listItem.findViewById(R.id.beidanchooseactlistitem_tv_d);// 平TextView
				final TextView tvL = (TextView) listItem.findViewById(R.id.beidanchooseactlistitem_tv_l);// 负TextView
				cbxW.setChecked((Boolean) beidanLotteryList.get(i).get(W));
				if (cbxW.isChecked()) {// 胜
					tvW.setTextColor(TEXT_CHECKED_COLOR);
					tvWPL.setTextColor(TEXT_CHECKED_COLOR);
				} else {
					tvW.setTextColor(TEXT_UNCHECKED_COLOR);
					tvWPL.setTextColor(TEXT_UNCHECKED_COLOR);
				}
				cbxD.setChecked((Boolean) beidanLotteryList.get(i).get(D));
				if (cbxD.isChecked()) {// 平
					tvD.setTextColor(TEXT_CHECKED_COLOR);
					tvDPL.setTextColor(TEXT_CHECKED_COLOR);
				} else {
					tvD.setTextColor(TEXT_UNCHECKED_COLOR);
					tvDPL.setTextColor(TEXT_UNCHECKED_COLOR);
				}
				cbxL.setChecked((Boolean) beidanLotteryList.get(i).get(L));
				if (cbxL.isChecked()) {// 负
					tvL.setTextColor(TEXT_CHECKED_COLOR);
					tvLPL.setTextColor(TEXT_CHECKED_COLOR);
				} else {
					tvL.setTextColor(TEXT_UNCHECKED_COLOR);
					tvLPL.setTextColor(TEXT_UNCHECKED_COLOR);
				}

				tvWPL.setText(beiDanAgainstInfo.getC1());
				tvDPL.setText(beiDanAgainstInfo.getC3());
				tvLPL.setText(beiDanAgainstInfo.getC5());
				ArrayList<CheckBox> checkBoxTemp = new ArrayList<CheckBox>();
				checkBoxTemp.add(cbxW);
				checkBoxTemp.add(cbxD);
				checkBoxTemp.add(cbxL);
				checkBoxTemp.add(cbxDan);

				danList.add(checkBoxTemp);

				listCbxDan.add(cbxDan);

				Button btnDelete = (Button) listItem.findViewById(R.id.beidanchooseactlistitem_btn_delete);
				btnDelete.setOnClickListener(this);
				OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						switch (buttonView.getId()) {
						case R.id.beidanchooseactlistitem_cbx_w:// 胜
							if (isChecked) {
								tvW.setTextColor(TEXT_CHECKED_COLOR);
								tvWPL.setTextColor(TEXT_CHECKED_COLOR);
							} else {
								tvW.setTextColor(TEXT_UNCHECKED_COLOR);
								tvWPL.setTextColor(TEXT_UNCHECKED_COLOR);
							}
							updateLotteryListItem(i_, isChecked, cbxD.isChecked(), cbxL.isChecked(), cbxDan);
							break;

						case R.id.beidanchooseactlistitem_cbx_d:// 平
							if (isChecked) {
								tvD.setTextColor(TEXT_CHECKED_COLOR);
								tvDPL.setTextColor(TEXT_CHECKED_COLOR);
							} else {
								tvD.setTextColor(TEXT_UNCHECKED_COLOR);
								tvDPL.setTextColor(TEXT_UNCHECKED_COLOR);
							}
							updateLotteryListItem(i_, cbxW.isChecked(), isChecked, cbxL.isChecked(), cbxDan);
							break;

						case R.id.beidanchooseactlistitem_cbx_l:// 负
							if (isChecked) {
								tvL.setTextColor(TEXT_CHECKED_COLOR);
								tvLPL.setTextColor(TEXT_CHECKED_COLOR);
							} else {
								tvL.setTextColor(TEXT_UNCHECKED_COLOR);
								tvLPL.setTextColor(TEXT_UNCHECKED_COLOR);
							}
							updateLotteryListItem(i_, cbxW.isChecked(), cbxD.isChecked(), isChecked, cbxDan);
							break;

						case R.id.beidanchooseactlistitem_cbx_dan:// 胆
							EDebug.o(BeidanBuyLottery.this, strGuoguan.substring(0, 1));
							if (isChecked) {
								updateTextViewSet();
								EDebug.o(BeidanBuyLottery.this, "limit dan num is " + getLimitDanNum());
								if (!cbxW.isChecked() && !cbxD.isChecked() && !cbxL.isChecked()) {// 如果胜平负都没选中，胆码不能选
									cbxDan.setChecked(false);
								} else if (getDanItemNum() > LIMIT_DAN_COUNT - 1 || getDanItemNum() > getActiveItemNum() - 2 || getDanItemNum() > getLimitDanNum() - 1) {
									cbxDan.setChecked(false);
									EDebug.o(BeidanBuyLottery.this, "limit dan num is " + getLimitDanNum());
								} else {
									EDebug.o(BeidanBuyLottery.this, "limit dan num is " + getLimitDanNum());
									BeidanLotteryInfo.getBeiDanLotteryInfo().getLotterList().get(i_).put(IS_DAN, true);
									if (getDanItemNum() == LIMIT_DAN_COUNT || getDanItemNum() == getActiveItemNum() - 1 || getDanItemNum() == getLimitDanNum()) {
										updateDanList(false);
									}
									updateTextViewSet();
								}
							} else {
								updateDanList(true);
								BeidanLotteryInfo.getBeiDanLotteryInfo().getLotterList().get(i_).put(IS_DAN, false);
								updateTextViewSet();
							}
							EDebug.o(BeidanBuyLottery.this, "dan num is " + getDanItemNum());
							break;

						default:
							break;
						}
					}
				};
				cbxW.setOnCheckedChangeListener(onCheckedChangeListener);
				cbxD.setOnCheckedChangeListener(onCheckedChangeListener);
				cbxL.setOnCheckedChangeListener(onCheckedChangeListener);
				cbxDan.setOnCheckedChangeListener(onCheckedChangeListener);
				linearList.addView(listItem, param);
			}
		}
	}

	/**
	 * @Title: updateDanList
	 * @Description: 更新胆CheckBox的状态
	 * @param: @param b 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void updateDanList(boolean b) {
		for (int i = 0; i < listCbxDan.size(); i++) {
			if (!listCbxDan.get(i).isChecked()) {
				listCbxDan.get(i).setEnabled(b);
			}
		}
	}

	/**
	 * @Title: handlerInit
	 * @Description: 初始化Handler
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void handlerInit() {

		Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				progressdialogNormalBuy.dismiss();
				switch (msg.what) {
				// 购买成功
				case BUY_SUCCEED:
					buySucceed();
					break;
				// 购买失败
				case BUY_FAIL:
					if (beiDanNormalBuyCommunicater.getState() == 0) {// 请查看购彩记录确认是否投注成功
						ViewerUtil.showTipDialog(context, beiDanNormalBuyCommunicater.getErrorMSG() + Config.COMMA + Language.POPLOTTERYTIP_LOOKOVERRECORD, false);
					} else if (beiDanNormalBuyCommunicater.getState() == 105) {// 余额不足
						popBalanceNotEnoughAlert();
					} else {// 失败
						// ViewerUtil.showTipDialog(context, beiDanNormalBuyCommunicater.getErrorMSG(), false);
						Toast.makeText(context, beiDanNormalBuyCommunicater.getErrorMSG(), Toast.LENGTH_SHORT).show();
					}

					break;
				// 连接失败
				case Config.CONNECTION_FAIL:
					ViewerUtil.showTipDialog(context, Language.STR_CONNECTIONFAIL + Config.COMMA + Language.POPLOTTERYTIP_LOOKOVERRECORD, false);
					break;

				default:
					break;
				}
			}
		};

		super.setHandler(handler);
	}

	/**
	 * @Title: init
	 * @Description: 初始化
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void init() {
		btnBack = (Button) view.findViewById(R.id.beidanchooseactlist_btn_back);
		btnGuoguan = (Button) view.findViewById(R.id.beidanchooseactlist_btn_guoguan);
		btnClean = (Button) view.findViewById(R.id.beidanchooseactlist_btn_clean);
		btnHemai = (Button) view.findViewById(R.id.beidanchooseactlist_btn_hemai);
		btnNormalBuy = (Button) view.findViewById(R.id.beidanchooseactlist_btn_normalBy);

		btnBack.setOnClickListener(this);
		btnGuoguan.setText("自由过关");
		btnGuoguan.setOnClickListener(this);
		btnClean.setOnClickListener(this);
		btnHemai.setOnClickListener(this);
		btnNormalBuy.setOnClickListener(this);

		tvGuoguan = (TextView) view.findViewById(R.id.beidanchooseactlist_tv_guoguan);
		tvChangCount = (TextView) view.findViewById(R.id.beidanchooseactlist_tv_changCount);
		tvBetCount = (TextView) view.findViewById(R.id.beidanchooseactlist_tv_betCount);
		tvMoney = (TextView) view.findViewById(R.id.beidanchooseactlist_tv_money);
		tvJackpot = (TextView) view.findViewById(R.id.beidanchooseactlist_tv_jackpot);
		tvJiezhiTime = (TextView) view.findViewById(R.id.beidanchooseactlist_tv_jiezhiTime);
		// tvBalance = (TextView) view.findViewById(R.id.beidanchooseactlist_tv_balance);
		edtBeitou = (EditText) view.findViewById(R.id.beidanchooseactlist_edt_beitou);

		linearList = (LinearLayout) view.findViewById(R.id.beidanchooseactlist_linear_list);
		setLimitDanNum(LIMIT_DAN_COUNT);

		beiDanNormalBuyCommunicater = new BeiDanBuyCommunicater();

		edtBeitou.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (!(String.valueOf(s).equals(Config.NULLMARK)) && s != null) {

					Toast partCountToast = Toast.makeText(context, Language.STR_QINGSHURU + Config.NUM1 + Config.HORIZONTAL_LINE + "100000" + Language.STR_ZHIJIANDESHUZI, Toast.LENGTH_SHORT);
					beishu = Integer.parseInt(String.valueOf(s));
					if (beishu > 100000) {
						partCountToast.show();
						beishu = 100000;
						edtBeitou.setText(Integer.toString(beishu));
					} else if (beishu < 1) {
						partCountToast.show();
						beishu = 1;
						edtBeitou.setText(Integer.toString(beishu));
					} else {

					}
				} else {
					beishu = 0;
				}
				updateTextViewSet();
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
		progressdialogNormalBuy = ViewerUtil.showProgressDialog(context, Language.POPLOTTERYTIP_BUYING);

	}

	/**
	 * @Title: threadNormalBuy
	 * @Description: 复式代购
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void threadNormalBuy() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					boolean b = beiDanNormalBuyCommunicater.isBuySucceed(beishu, ((long) beishu * (long) zhushu * 2), guoguanType, codes, danma, sgtype, strGuoguan, 0, expect, Config.NUM1,
							Config.NUM1, 0, Config.NUM0, Config.NUM0, version);
					if (b) {
						Message msg = handlerChooseList.obtainMessage(BUY_SUCCEED, null);
						handlerChooseList.sendMessage(msg);
					} else {
						Message msg = handlerChooseList.obtainMessage(BUY_FAIL, null);
						handlerChooseList.sendMessage(msg);
					}
				} catch (Exception e) {
					Message msg = handlerChooseList.obtainMessage(Config.CONNECTION_FAIL, null);
					handlerChooseList.sendMessage(msg);
				}
			}
		}.start();
	}

	public View getView() {
		BeidanChooseAct.pageFlag = 1;
		updateListView();
		dataInit();
		updateTextViewSet();
		edtBeitou.setText(Integer.toString(beishu));
		Message msg = BeidanAgp.handlerBeidanAgp.obtainMessage(BeidanAgp.IN_LIST_VIEW, null);
		BeidanAgp.handlerBeidanAgp.sendMessage(msg);
		return view;
	}

	/**
	 * @Title: setViewChoosePage
	 * @Description:
	 * @param: @param choosePage 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	public void setViewChoosePage(BeidanChooseList choosePage) {
		this.chooseListPage = choosePage;
	}

	@Override
	public void onClick(View v) {
		Message msg = BeidanChooseList.handlerChoosePage.obtainMessage(BeidanChooseList.DO_BACK_UPDATE, null);
		switch (v.getId()) {
		// 后退
		case R.id.beidanchooseactlist_btn_back:
			BeidanChooseList.handlerChoosePage.sendMessage(msg);
			((Activity) context).setContentView(chooseListPage.getView());
			break;
		// 清理
		case R.id.beidanchooseactlist_btn_clean:
			cleanTheList();
			BeidanChooseList.handlerChoosePage.sendMessage(msg);
			((Activity) context).setContentView(chooseListPage.getView());
			break;
		// 过关方式
		case R.id.beidanchooseactlist_btn_guoguan:
			updateGuoguanTypeList();
			showDialogGuoguan();
			break;
		// 合买
		case R.id.beidanchooseactlist_btn_hemai:
			checkHemai();
			break;
		// 复式代购
		case R.id.beidanchooseactlist_btn_normalBy:
			checkNomalBuy();
			break;
		// 删除对阵
		case R.id.beidanchooseactlistitem_btn_delete:
			linearList.removeView((View) v.getParent());
			deleteLotteryListItem((Integer) ((View) v.getParent()).getTag());
			updateTextViewSet();
			if (BeidanLotteryInfo.getBeiDanLotteryInfo().getActiveTeamCount() < 1) {
				BeidanChooseList.handlerChoosePage.sendMessage(msg);
				((Activity) context).setContentView(chooseListPage.getView());
			} else if (getActiveItemNum() < getDanItemNum()) {
				strGuoguan = Config.NULLMARK;
				listSGType.clear();
				cleanDanItem();
			}
			updateGuoGuanType();
			break;

		default:
			break;
		}

	}

	/**
	 * @Title: checkHemai
	 * @Description: 检查是否合买
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void checkHemai() {
		updateNormalBy();
		BeidanLotteryInfo beidanLotteryInfo = BeidanLotteryInfo.getBeiDanLotteryInfo();
		beidanLotteryInfo.setCodes(codes);
		beidanLotteryInfo.setDanma(danma);
		beidanLotteryInfo.setGuoguanType(guoguanType);
		beidanLotteryInfo.setMultiple(beishu);
		beidanLotteryInfo.setSgType(sgtype);
		beidanLotteryInfo.setZhushuCount(zhushu);
		beidanLotteryInfo.setExpect(expect);
		beidanLotteryInfo.setSgtypename(strGuoguan);
		Intent intentToHemai = new Intent();
		intentToHemai.setClass(context, BeidanFaqiHemaiAct.class);
		if (beishu > 0 && zhushu > 0) {
			if (UserInfo.getUserInfo().getUserSession().length() < 3) {
				Intent intent = new Intent();
				intent.setClass(context, LoginAct.class);
				((Activity) context).startActivity(intent);
			} else {
				((Activity) context).startActivity(intentToHemai);
			}
		} else {
			ViewerUtil.showTipDialog(context, Language.MSG_ZHENGQUE_TIANXIE, false);
		}
	}

	/**
	 * @Title: checkNomalBuy
	 * @Description:检查复式代购，登陆超时跳转到登陆界面，正常则发起购买
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	private void checkNomalBuy() {
		if (updateNormalBy()) {
			if (beishu > 0 && zhushu > 0) {
				if (UserInfo.getUserInfo().getUserSession().length() < 3) {
					Intent intent = new Intent();
					intent.setClass(context, LoginAct.class);
					((Activity) context).startActivity(intent);
				} else {
					LoginTimeControl test = new LoginTimeControl();
					if (test.isNeedInputPwd()) {
						test.showInputPasswordDialog(context);
					} else {
						progressdialogNormalBuy.show();
						threadNormalBuy();
					}
				}
			} else if (!(zhushu > 0)) {
				ViewerUtil.showTipDialog(((Activity) context), Language.MSG_SELECT_GUOGUAN, false);
			} else {
				ViewerUtil.showTipDialog(((Activity) context), Language.MSG_ZHENGQUE_TIANXIE, false);
			}
		}
	}

	/**
	 * @Title: updateNormalBy
	 * @Description: 复式代购参数计算
	 * @param: @return 设定文件
	 * @return: boolean 返回类型
	 * @throws
	 */
	private boolean updateNormalBy() {

		if (this.getActiveItemNum() < 2) {
			ViewerUtil.showTipDialog(((Activity) context), Language.MSG_LEST_TWOTEAM, false);
			return false;
		}
		StringBuffer sbCodes = new StringBuffer();
		StringBuffer sbDanma = new StringBuffer();
		BeidanLotteryInfo beiDanLotteryInfo = BeidanLotteryInfo.getBeiDanLotteryInfo();
		ArrayList<Hashtable<String, Object>> lotteryinfo = new ArrayList<Hashtable<String, Object>>();
		lotteryinfo = beiDanLotteryInfo.getLotterList();
		for (int i = 0; i < lotteryinfo.size(); i++) {
			Hashtable<String, Object> hashtable = new Hashtable<String, Object>();
			hashtable = lotteryinfo.get(i);
			if ((Boolean) hashtable.get(IS_ACTIVE)) {
				BeidanAgainstInfo beiDanAgainstInfo = (BeidanAgainstInfo) hashtable.get(INFO);
				sbCodes.append(beiDanAgainstInfo.getOrderNum()).append(Config.MAOHAO).append(Config.LEFT_BR);
				if ((Boolean) hashtable.get(W)) {
					sbCodes.append(Language.STR_WIN);
				}
				if ((Boolean) hashtable.get(D)) {
					if ((Boolean) hashtable.get(W)) {
						sbCodes.append(Config.COMMA).append(Language.STR_DRAW);
					} else {
						sbCodes.append(Language.STR_DRAW);
					}
				}
				if ((Boolean) hashtable.get(L)) {
					if ((Boolean) hashtable.get(W) || (Boolean) hashtable.get(D)) {
						sbCodes.append(Config.COMMA).append(Language.STR_LOST);
					} else {
						sbCodes.append(Language.STR_LOST);
					}
				}
				sbCodes.append(Config.RIGHT_BR).append(Config.SLASH_LINE);
				// 判断是否选胆码
				if ((Boolean) hashtable.get(IS_DAN)) {
					sbDanma.append(beiDanAgainstInfo.getOrderNum()).append(Config.MAOHAO).append(Config.LEFT_BR);
					if ((Boolean) hashtable.get(W)) {
						sbDanma.append(Language.STR_WIN);
					}
					if ((Boolean) hashtable.get(D)) {
						if ((Boolean) hashtable.get(W)) {
							sbDanma.append(Config.COMMA).append(Language.STR_DRAW);
						} else {
							sbDanma.append(Language.STR_DRAW);
						}
					}
					if ((Boolean) hashtable.get(L)) {
						if ((Boolean) hashtable.get(W) || (Boolean) hashtable.get(D)) {
							sbDanma.append(Config.COMMA).append(Language.STR_LOST);
						} else {
							sbDanma.append(Language.STR_LOST);
						}
					}
					sbDanma.append(Config.RIGHT_BR).append(Config.SLASH_LINE);
				}

			}

		}
		sbCodes.deleteCharAt(sbCodes.length() - 1);
		if (sbDanma.length() != 0) {
			sbDanma.deleteCharAt(sbDanma.length() - 1);
		}
		codes = sbCodes.toString();
		EDebug.o(BeidanBuyLottery.this, "the codes is " + codes);
		zhushu = getZhushuCount(beiDanLotteryInfo.getActiveTeamCount(), beiDanLotteryInfo.getLotterList(), listSGType);
		EDebug.o(BeidanBuyLottery.this, "the betCount is " + zhushu);
		danma = sbDanma.toString();
		EDebug.o(BeidanBuyLottery.this, "the danma is " + danma);
		return true;
	}

	/** 更新textview显示 */
	private void updateTextViewSet() {
		BeidanLotteryInfo beiDanLotteryInfo = BeidanLotteryInfo.getBeiDanLotteryInfo();
		tvGuoguan.setText(strGuoguan);
		tvChangCount.setText(Integer.toString((Integer) beiDanLotteryInfo.getActiveTeamCount()));
		zhushu = getZhushuCount(beiDanLotteryInfo.getActiveTeamCount(), beiDanLotteryInfo.getLotterList(), listSGType);
		tvBetCount.setText(Integer.toString(zhushu));
		DecimalFormat f = new DecimalFormat("0.00");

		tvMoney.setText(Html.fromHtml("总金额<font color = red>" + Language.POPLOTTERYTIP_RMB + f.format((long) zhushu * (long) beishu * 2) + "</font>" + ",投注后余额" + "<font color = red>"
				+ Language.POPLOTTERYTIP_RMB + com.esun.util.LotteryUtils.getLotRestBottom(UserInfo.getUserInfo().getUserBalance(), zhushu * beishu * 2) + "</font>"));
		tvJackpot.setText(Html.fromHtml("<font color = red>" + Language.POPLOTTERYTIP_RMB + getMaxMoney(beiDanLotteryInfo.getActiveTeamCount(), beiDanLotteryInfo.getLotterList(), listSGType)
				+ "</font>"));
		for (int i = 0; i < beiDanLotteryInfo.getLotterList().size(); i++) {
			if ((Boolean) beiDanLotteryInfo.getLotterList().get(i).get(IS_ACTIVE)) {
				tvJiezhiTime.setText(((BeidanAgainstInfo) beiDanLotteryInfo.getLotterList().get(i).get(INFO)).getEndTime());
				break;
			}
		}
		// tvBalance.setText(Html.fromHtml("<font color = red>" + Language.POPLOTTERYTIP_RMB + com.esun.util.LotteryUtils.getLotRestBottom(UserInfo.getUserInfo().getUserBalance(),
		// zhushu * beishu * 2)+ "</font>"));
	}

	/** 获取注数 */
	private int getZhushuCount(int activeTeamCount, ArrayList<Hashtable<String, Object>> lotteryList, ArrayList<Hashtable<String, Object>> listSgType) {
		int zhushu_ = 0;

		if (activeTeamCount != 0) {
			int teamCount = activeTeamCount;
			int[][] pDetail;
			pDetail = new int[teamCount][2];
			int tCount = 0;
			for (int i = 0; i < lotteryList.size(); i++) {
				if ((Boolean) lotteryList.get(i).get(IS_ACTIVE)) {
					if ((Boolean) lotteryList.get(i).get(IS_DAN)) {
						pDetail[tCount][0] = 1;
					} else {
						pDetail[tCount][0] = 0;
					}
					int checkCount = 0;
					if ((Boolean) lotteryList.get(i).get(W)) {
						checkCount += 1;
					}
					if ((Boolean) lotteryList.get(i).get(D)) {
						checkCount += 1;
					}
					if ((Boolean) lotteryList.get(i).get(L)) {
						checkCount += 1;
					}
					pDetail[tCount][1] = checkCount;
					tCount += 1;
				}
			}
			if (guoguanType == 3) {
				int[] pGGType;
				pGGType = new int[16];
				for (int i = 0; i < 16; i++) {
					pGGType[i] = 0;
				}
				for (int i = 0; i < listSgType.size(); i++) {
					if ((Boolean) listSgType.get(i).get(IS_CHECKED)) {
						pGGType[i + 1] = 1;
						EDebug.o(BeidanBuyLottery.this, "checked item is " + i);
					}
				}

				zhushu_ = ESInterface.getZYTouZhuNum(teamCount, pDetail, pGGType, Integer.parseInt(Config.BEIJINGZUQIUDANCHANG));
			} else if (guoguanType == 2) {
				int[] pGGType;
				pGGType = new int[2];
				for (int i = 0; i < listSgType.size(); i++) {
					if ((Boolean) listSgType.get(i).get(IS_CHECKED)) {
						pGGType[0] = (Integer) listSgType.get(i).get(TEAM_COUNT);
						pGGType[1] = (Integer) listSgType.get(i).get(TYPE);
						EDebug.o(BeidanBuyLottery.this, "team count is " + pGGType[0] + " type is " + pGGType[1]);
					}
				}
				zhushu_ = ESInterface.getDCTouZhuNum(teamCount, pDetail, pGGType, Integer.parseInt(Config.BEIJINGZUQIUDANCHANG));
				EDebug.o("zhushu = " + zhushu_);
			} else {
				zhushu_ = 0;
			}

		}

		return zhushu_;
	}

	/** 获取最大中奖金额 */
	private String getMaxMoney(int activeTeamCount, ArrayList<Hashtable<String, Object>> lotteryList, ArrayList<Hashtable<String, Object>> listSgType) {
		EDebug.o(BeidanBuyLottery.this, " max money 走你");

		// Object[] objTmp = { new TouZhuData(), new TouZhuData(), new TouZhuData(), new TouZhuData(),
		// new TouZhuData(), new TouZhuData(), new TouZhuData(), new TouZhuData(), new TouZhuData(),
		// new TouZhuData(), new TouZhuData(), new TouZhuData(), new TouZhuData(), new TouZhuData(),
		// new TouZhuData() };
		ArrayList<Hashtable<String, Object>> listActive = new ArrayList<Hashtable<String, Object>>();
		for (int i = 0; i < lotteryList.size(); i++) {
			if ((Boolean) lotteryList.get(i).get(IS_ACTIVE)) {
				listActive.add(lotteryList.get(i));
			}
		}
		EDebug.o(BeidanBuyLottery.this, " max money 走着");
		final int touzhuSize = listActive.size();

		TouZhuData[] objTmp = new TouZhuData[touzhuSize];

		for (int i = 0; i < touzhuSize; i++) {
			TouZhuData tzd = new TouZhuData();
			objTmp[i] = tzd;
			tzd.isEff = 1;
			if ((Boolean) listActive.get(i).get(IS_DAN)) {// 是否胆拖
				tzd.isDan = 1;
			} else {
				tzd.isDan = 0;
			}

			if ((Boolean) listActive.get(i).get(W)) {// 胜
				try {
					tzd.winPL = (float) Double.parseDouble(((BeidanAgainstInfo) listActive.get(i).get(INFO)).getC1());
				} catch (Exception e) {
					tzd.winPL = 0;
				}
			} else {
				tzd.winPL = 0;
			}
			if ((Boolean) listActive.get(i).get(D)) {// 平
				try {
					tzd.drawPL = (float) Double.parseDouble(((BeidanAgainstInfo) listActive.get(i).get(INFO)).getC3());
				} catch (Exception e) {
					tzd.drawPL = 0;
				}
			} else {
				tzd.drawPL = 0;
			}
			if ((Boolean) listActive.get(i).get(L)) {// 负
				try {
					tzd.losePL = (float) Double.parseDouble(((BeidanAgainstInfo) listActive.get(i).get(INFO)).getC5());
				} catch (Exception e) {
					tzd.losePL = 0;
				}
			} else {
				tzd.losePL = 0;
			}
		}
		double maxMoney = 0;
		if (activeTeamCount != 0) {
			if (guoguanType == 3) {
				EDebug.o(BeidanBuyLottery.this, " max money  get type is 3");
				int[] pGGType = new int[16];
				for (int i = 0; i < 16; i++) {
					pGGType[i] = 0;
				}
				for (int i = 0; i < listSgType.size(); i++) {
					if ((Boolean) listSgType.get(i).get(IS_CHECKED)) {
						pGGType[i + 1] = 1;
						EDebug.o(BeidanBuyLottery.this, "checked item is " + i);
					}
				}

				// for(Object match : objTmp){
				// TouZhuData data = (TouZhuData)match;
				// EDebug.o("win = "+data.winPL+", draw = "+ data.drawPL +", lose = "+data.losePL);
				// }
				// for(int type : pGGType){
				// EDebug.o("type = "+type);
				// }

				EDebug.o(BeidanBuyLottery.this, " max money 调用");
				EDebug.o(this, objTmp.toString() + pGGType.toString() + beishu + activeTeamCount);
				EDebug.o(this, pGGType.toString() + "===" + beishu + "===" + activeTeamCount);
				maxMoney = ESInterface.getZYGGMaxMoney(objTmp, pGGType, beishu, activeTeamCount, Integer.parseInt(Config.BEIJINGZUQIUDANCHANG));
				EDebug.o(BeidanBuyLottery.this, " max money 完成");
			} else if (guoguanType == 2) {
				EDebug.o(BeidanBuyLottery.this, " max money  get type is 2");
				int[] pGGType;
				pGGType = new int[2];
				for (int i = 0; i < listSgType.size(); i++) {
					if ((Boolean) listSgType.get(i).get(IS_CHECKED)) {
						pGGType[0] = (Integer) listSgType.get(i).get(TEAM_COUNT);
						pGGType[1] = (Integer) listSgType.get(i).get(TYPE);
						EDebug.o(BeidanBuyLottery.this, "team count is " + pGGType[0] + " type is " + pGGType[1]);
					}
				}
				EDebug.o(BeidanBuyLottery.this, " max money 调用");
				maxMoney = ESInterface.getDCGGMaxMoney(objTmp, pGGType, beishu, activeTeamCount, Integer.parseInt(Config.BEIJINGZUQIUDANCHANG));
				EDebug.o(BeidanBuyLottery.this, " max money 完成");
			} else {

			}

		}
		DecimalFormat f = new DecimalFormat("0.00");
		EDebug.o(BeidanBuyLottery.this, " max money is" + maxMoney);
		return f.format(maxMoney);
	}

	/** 清除投注列表 */
	private void cleanTheList() {
		linearList.removeAllViews();
		ArrayList<Hashtable<String, Object>> beiDanLotteryList = new ArrayList<Hashtable<String, Object>>();
		beiDanLotteryList = BeidanLotteryInfo.getBeiDanLotteryInfo().getLotterList();
		for (int i = 0; i < beiDanLotteryList.size(); i++) {
			Hashtable<String, Object> beiDanLotteryTable = new Hashtable<String, Object>();
			beiDanLotteryTable = beiDanLotteryList.get(i);
			if ((Boolean) beiDanLotteryTable.get(IS_ACTIVE)) {
				beiDanLotteryTable.put(IS_ACTIVE, false);
				beiDanLotteryTable.put(W, false);
				beiDanLotteryTable.put(D, false);
				beiDanLotteryTable.put(L, false);
				beiDanLotteryTable.put(IS_DAN, false);
			}
		}
	}

	/** 删除第position个队伍 */
	private void deleteLotteryListItem(int position) {
		Hashtable<String, Object> beiDanLotteryTable = new Hashtable<String, Object>();
		beiDanLotteryTable = BeidanLotteryInfo.getBeiDanLotteryInfo().getLotterList().get(position);
		beiDanLotteryTable.put(W, false);
		beiDanLotteryTable.put(D, false);
		beiDanLotteryTable.put(L, false);
		beiDanLotteryTable.put(IS_ACTIVE, false);
		beiDanLotteryTable.put(IS_DAN, false);
	}

	/** 更新第position个投注队伍 */
	private void updateLotteryListItem(int position, Boolean w, Boolean d, Boolean l, CheckBox cbxDan) {
		Hashtable<String, Object> beiDanLotteryTable = new Hashtable<String, Object>();
		beiDanLotteryTable = BeidanLotteryInfo.getBeiDanLotteryInfo().getLotterList().get(position);
		beiDanLotteryTable.put(IS_ACTIVE, true);
		beiDanLotteryTable.put(W, w);
		beiDanLotteryTable.put(D, d);
		beiDanLotteryTable.put(L, l);
		cbxDan.setEnabled(true);
		if (!w && !d && !l) {
			beiDanLotteryTable.put(IS_ACTIVE, false);
			cbxDan.setEnabled(false);
			cbxDan.setChecked(false);
			beiDanLotteryTable.put(IS_DAN, false);
			updateGuoGuanType();
		}
		EDebug.o(this, BeidanLotteryInfo.getBeiDanLotteryInfo().getActiveTeamCount());
		EDebug.o(BeidanBuyLottery.this, "the active item count is " + BeidanLotteryInfo.getBeiDanLotteryInfo().getActiveTeamCount());
		updateTextViewSet();
	}

	/** 清除胆码 */
	private void cleanDanItem() {
		ArrayList<Hashtable<String, Object>> beiDanLotteryList = new ArrayList<Hashtable<String, Object>>();
		beiDanLotteryList = BeidanLotteryInfo.getBeiDanLotteryInfo().getLotterList();
		for (int i = 0; i < beiDanLotteryList.size(); i++) {
			Hashtable<String, Object> beiDanLotteryTable = new Hashtable<String, Object>();
			beiDanLotteryTable = beiDanLotteryList.get(i);
			if ((Boolean) beiDanLotteryTable.get(IS_DAN)) {
				beiDanLotteryTable.put(IS_DAN, false);
			}
		}
		for (int i = 0; i < listCbxDan.size(); i++) {
			((CheckBox) listCbxDan.get(i)).setChecked(false);
		}
		updateTextViewSet();
	}

	/** 获取可用队伍个数 */
	public int getActiveItemNum() {
		int count = 0;
		ArrayList<Hashtable<String, Object>> beiDanLotteryList = new ArrayList<Hashtable<String, Object>>();
		beiDanLotteryList = BeidanLotteryInfo.getBeiDanLotteryInfo().getLotterList();
		for (int i = 0; i < beiDanLotteryList.size(); i++) {
			if ((Boolean) beiDanLotteryList.get(i).get(IS_ACTIVE)) {
				count++;
			}
		}
		return count;
	}

	/** 获取已选中胆码数量 */
	private int getDanItemNum() {
		int count = 0;
		ArrayList<Hashtable<String, Object>> beiDanLotteryList = new ArrayList<Hashtable<String, Object>>();
		beiDanLotteryList = BeidanLotteryInfo.getBeiDanLotteryInfo().getLotterList();
		for (int i = 0; i < beiDanLotteryList.size(); i++) {
			if ((Boolean) beiDanLotteryList.get(i).get(IS_DAN)) {
				count += 1;
			}
		}
		return count;
	}

	/** 显示过关对话框 */
	private void showDialogGuoguan() {
		if (dialogGuoguan == null) {
			dialogGuoguan = new Dialog(context, R.style.dialog);
		}
		dialogGuoguan.getWindow().setContentView(R.layout.beidan_dialog_guoguan_type);

		limitDanForDialogGuoguan = getDanItemNum();

		gridViewZiyou = (GridViewNoScroll) dialogGuoguan.getWindow().findViewById(R.id.beidandialogguoguantype_grid_ziyou);
		gridViewDuochuan = (GridViewNoScroll) dialogGuoguan.getWindow().findViewById(R.id.beidandialogguoguantype_grid_duochuan);
		rbZiyou = (RadioButton) dialogGuoguan.getWindow().findViewById(R.id.beidandialogguoguantype_rb_ziyou);
		ZiyouListAdapter ziyouListAdapter = new ZiyouListAdapter(context);
		gridViewZiyou.setAdapter(ziyouListAdapter);
		duochuanListAdapter = new DuochuanListAdapter(listDuochuan);
		gridViewDuochuan.setAdapter(duochuanListAdapter);
		gridViewDuochuan.setVisibility(View.GONE);

		RadioGroup radioGroupTitle = (RadioGroup) dialogGuoguan.getWindow().findViewById(R.id.beidandialogguoguantype_radiogroup_main);
		radioGroupTitle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				// 自由过关
				case R.id.beidandialogguoguantype_rb_ziyou:
					gridViewDuochuan.setVisibility(View.GONE);
					gridViewZiyou.setVisibility(View.VISIBLE);
					break;
				// 多串过关
				case R.id.beidandialogguoguantype_rb_duochuan:
					gridViewDuochuan.setVisibility(View.VISIBLE);
					gridViewZiyou.setVisibility(View.GONE);
					break;
				default:
					break;
				}
			}
		});

		OnClickListener onClickListener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				// 提交
				case R.id.beidandialogguoguantype_btn_submit:
					guoguanSubmit();
					break;
				// 取消
				case R.id.beidandialogguoguantype_btn_cancel:
					dialogGuoguan.dismiss();
					break;

				default:
					break;
				}
			}
		};
		Button btnSubmit = (Button) dialogGuoguan.getWindow().findViewById(R.id.beidandialogguoguantype_btn_submit);
		Button btnCancel = (Button) dialogGuoguan.getWindow().findViewById(R.id.beidandialogguoguantype_btn_cancel);
		btnCancel.setOnClickListener(onClickListener);
		btnSubmit.setOnClickListener(onClickListener);
		dialogGuoguan.show();
	}

	/** 过关方式提交 */
	private void guoguanSubmit() {
		if (rbZiyou.isChecked()) {
			updateZiyouLimitDanNum();
		} else {
			updateDuochuanLimitDanNum();
		}
		EDebug.o(BeidanBuyLottery.this, "limit dan num is " + getLimitDanNum());

	}

	/** 更新多穿过关胆码上限 */
	private void updateDuochuanLimitDanNum() {
		int limitDan = 8;
		boolean b = false;
		StringBuffer sb = new StringBuffer();
		StringBuffer sbSgType = new StringBuffer();
		for (int i = 0; i < listDuochuan.size(); i++) {
			if ((Boolean) listDuochuan.get(i).get(IS_CHECKED)) {
				b = true;
				limitDan = (Integer) listDuochuan.get(i).get(TEAM_COUNT);
				sb.append((String) listDuochuan.get(i).get(STR_TYPE));
				sbSgType.append(Integer.toString((Integer) listDuochuan.get(i).get(TAG)));
			}
		}
		if (b) {
			dialogGuoguan.dismiss();
			if (limitDanForDialogGuoguan == 0) {
				cleanDanItem();
				setListCbxDan(false);
			}
			setLimitDanNum(limitDan - 1);
			btnGuoguan.setText(Language.MSG_GUOGUAN_DUOCHUAN);
			strGuoguan = sb.toString();
			tvGuoguan.setText(strGuoguan);
			guoguanType = 2;
			sgtype = sbSgType.toString();
			EDebug.o(BeidanBuyLottery.this, "guoguanType is " + guoguanType + " && sgtype is [" + sgtype + "]");
			listSGType.clear();
			listSGType.addAll(listDuochuan);
			updateTextViewSet();
		} else {
			Toast.makeText(context, Language.MSG_PLEASE_SELECT, Toast.LENGTH_SHORT).show();
		}
	}

	/** 更新自由过关胆码上限 */
	private void updateZiyouLimitDanNum() {
		int limitDan = 14;
		int count = 0;
		StringBuffer sb = new StringBuffer();
		StringBuffer sbSgType = new StringBuffer();
		for (int i = 0; i < listZiyou.size(); i++) {
			if ((Boolean) listZiyou.get(i).get(IS_CHECKED)) {
				count += 1;
				sb.append((String) listZiyou.get(i).get(STR_TYPE)).append(Config.COMMA);
				sbSgType.append(Integer.toString((Integer) listZiyou.get(i).get(TAG))).append(Config.COMMA);

				if ((Integer) listZiyou.get(i).get(TEAM_COUNT) < limitDan) {
					limitDan = (Integer) listZiyou.get(i).get(TEAM_COUNT);
				}
			}
		}
		if (count == 0) {
			Toast.makeText(context, Language.MSG_PLEASE_SELECT, Toast.LENGTH_SHORT).show();
		} else {
			if (limitDanForDialogGuoguan == 0) {
				cleanDanItem();
			}
			listSGType.clear();
			listSGType.addAll(listZiyou);
			if (limitDan == getActiveItemNum()) {
				setLimitDanNum(0);
				setListCbxDan(false);
			} else {
				setListCbxDan(true);
				setLimitDanNum(limitDan - 1);
			}
			dialogGuoguan.dismiss();
			btnGuoguan.setText(Language.MSG_GUOGUAN_ZIYOU);
			strGuoguan = sb.toString();
			strGuoguan = strGuoguan.substring(0, strGuoguan.length() - 1);
			tvGuoguan.setText(strGuoguan);
			guoguanType = 3;
			sgtype = sbSgType.toString();
			sgtype = sgtype.substring(0, sgtype.length() - 1);
			updateTextViewSet();
			EDebug.o(BeidanBuyLottery.this, "guoguanType is " + guoguanType + " && sgtype is [" + sgtype + "]");
		}
	}

	/** 更新过关方式列表 */
	private void updateGuoguanTypeList() {
		listDuochuan.clear();
		listZiyou.clear();

		if (getActiveItemNum() > 0) {
			listZiyou.add(addItemTable(27, 1, 1));
			if (getActiveItemNum() > 1) {
				listZiyou.add(addItemTable(1, 2, 1));
				listDuochuan.add(addItemTable(2, 2, 3));
				if (getActiveItemNum() > 2) {
					listZiyou.add(addItemTable(3, 3, 1));
					listDuochuan.add(addItemTable(5, 3, 4));
					listDuochuan.add(addItemTable(6, 3, 7));
					if (getActiveItemNum() > 3) {
						listZiyou.add(addItemTable(7, 4, 1));
						listDuochuan.add(addItemTable(9, 4, 5));
						listDuochuan.add(addItemTable(12, 4, 11));
						listDuochuan.add(addItemTable(13, 4, 15));
						if (getActiveItemNum() > 4) {
							listZiyou.add(addItemTable(14, 5, 1));
							listDuochuan.add(addItemTable(28, 5, 6));
							listDuochuan.add(addItemTable(29, 5, 16));
							listDuochuan.add(addItemTable(18, 5, 26));
							listDuochuan.add(addItemTable(19, 5, 31));
							if (getActiveItemNum() > 5) {
								listZiyou.add(addItemTable(20, 6, 1));
								listDuochuan.add(addItemTable(30, 6, 7));
								listDuochuan.add(addItemTable(31, 6, 22));
								listDuochuan.add(addItemTable(32, 6, 42));
								listDuochuan.add(addItemTable(25, 6, 57));
								listDuochuan.add(addItemTable(26, 6, 63));
								if (getActiveItemNum() > 6) {
									listZiyou.add(addItemTable(35, 7, 1));
									if (getActiveItemNum() > 7) {
										listZiyou.add(addItemTable(36, 8, 1));
										if (getActiveItemNum() > 8) {
											listZiyou.add(addItemTable(37, 9, 1));
											if (getActiveItemNum() > 9) {
												listZiyou.add(addItemTable(38, 10, 1));
												if (getActiveItemNum() > 10) {
													listZiyou.add(addItemTable(39, 11, 1));
													if (getActiveItemNum() > 11) {
														listZiyou.add(addItemTable(40, 12, 1));
														if (getActiveItemNum() > 12) {
															listZiyou.add(addItemTable(41, 13, 1));
															if (getActiveItemNum() > 13) {
																listZiyou.add(addItemTable(42, 14, 1));
																if (getActiveItemNum() > 14) {
																	listZiyou.add(addItemTable(43, 15, 1));
																}
															}
														}
													}
												}
											}
										}

									}

								}
							}
						}

					}
				}
			}
		} else {
			listDuochuan.clear();
			listZiyou.clear();
		}
	}

	/**
	 * @title Hashtable
	 * @description 添加一项过关选项
	 * @param tag
	 *            过关选项标记
	 * @param teamCount
	 *            组数
	 * @param type
	 *            过关类型
	 */
	private Hashtable<String, Object> addItemTable(int tag, int teamCount, int type) {
		Hashtable<String, Object> table = new Hashtable<String, Object>();
		table.put(TAG, tag);
		table.put(TEAM_COUNT, teamCount);
		table.put(TYPE, type);
		String strType = null;
		if (teamCount == 1 && type == 1) {
			strType = "单关";
		} else {
			strType = Integer.toString(teamCount) + Language.MSG_CHUAN + Integer.toString(type);
		}
		table.put(STR_TYPE, strType);
		table.put(IS_CHECKED, false);
		return table;
	}

	/**
	 * @className ZiyouListAdapter
	 * @description 对话框中自由过关GridView的适配器
	 * @author Kael gaojian@500wan.com
	 */
	class ZiyouListAdapter extends BaseAdapter {
		private LayoutInflater listInflater;

		public ZiyouListAdapter(Context context) {
			this.listInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return listZiyou.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			convertView = (CheckBox) listInflater.inflate(R.layout.beidan_dialog_guoguan_ziyou_item, null);
			final CheckBox cbxItem = (CheckBox) convertView.findViewById(R.id.beidandialogguoguanziyouitem_cbx_item);
			cbxItem.setText((String) listZiyou.get(position).get(STR_TYPE));
			OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (!((Integer) listZiyou.get(position).get(TEAM_COUNT) > limitDanForDialogGuoguan)) {
						cbxItem.setChecked(false);
					} else {
						listZiyou.get(position).put(IS_CHECKED, isChecked);
						if (getActiveItemNum() < 9 && (Integer) listZiyou.get(position).get(TEAM_COUNT) == getActiveItemNum()) {
							limitDanForDialogGuoguan = 0;
						}
					}

				}
			};
			cbxItem.setOnCheckedChangeListener(onCheckedChangeListener);

			return convertView;
		}
	}

	/**
	 * @className DuochuanListAdapter
	 * @description 对话框中多串过关GridView的适配器
	 * @author Kael gaojian@500wan.com
	 */
	class DuochuanListAdapter extends BaseAdapter {

		ArrayList<Hashtable<String, Object>> list = new ArrayList<Hashtable<String, Object>>();

		public DuochuanListAdapter(ArrayList<Hashtable<String, Object>> dataList) {
			this.list = dataList;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Hashtable<String, Object> getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			final int index = position;

			if (convertView == null) {
				convertView = (CheckBox) LayoutInflater.from(context).inflate(R.layout.beidan_dialog_guoguan_duochuan_item, null);
			}
			final CheckBox cbxDuochuan = (CheckBox) convertView.findViewById(R.id.beidandialogguoguanduochuanitem_rb_item);
			cbxDuochuan.setChecked((Boolean) list.get(position).get(IS_CHECKED));
			cbxDuochuan.setText((String) list.get(position).get(STR_TYPE));

			cbxDuochuan.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 判断点击的是不是自己
					if (lastItem != index) {
						// 判断之前是否有被选中的按钮
						if (lastItem >= 0) {
							// 将所有按钮重置
							for (int i = 0; i < duochuanListAdapter.getCount(); i++) {
								Hashtable<String, Object> each = (Hashtable<String, Object>) duochuanListAdapter.getItem(i);
								each.put(IS_CHECKED, false);
								EDebug.o("把第" + i + "个改成不选中状态");
							}
						}
						// 将被选中按钮标记并置为选中
						lastItem = index;
						Hashtable<String, Object> each = (Hashtable<String, Object>) duochuanListAdapter.getItem(lastItem);
						each.put(IS_CHECKED, true);
						EDebug.o("把第" + lastItem + "个改成选中状态");
					} else {
						// 如果点击的是自己，则将自己置为选中状态
						Hashtable<String, Object> each = (Hashtable<String, Object>) duochuanListAdapter.getItem(index);
						each.put(IS_CHECKED, true);
						EDebug.o("点击的自己");
					}
					duochuanListAdapter.notifyDataSetChanged();
				}
			});

			return convertView;
		}

	}

	private Dialog dialogBuySucceed;

	/** 购买成功 */
	private void buySucceed() {
		String yue = beiDanNormalBuyCommunicater.getResult().get(Key.KEY_USERMONEY);
		UserInfo.getUserInfo().setUserBalance(yue);
		dialogBuySucceed = new Dialog(context, R.style.dialog);
		dialogBuySucceed.setContentView(R.layout.viewerutil_dialog_showbuysucceed);
		// 提示语
		TextView tvMsg = (TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_msg);
		tvMsg.setText(Language.MSG_BEIDAN_NORMALBUY_SUCCEED);
		DecimalFormat f = new DecimalFormat("0.00");
		// 注数
		TextView tvTerm = (TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_term);
		tvTerm.setText(Language.POPLOTTERYTIP_ZHUSHU + Config.MAOHAO + Integer.toString(zhushu));
		// 倍投
		TextView tvCount = (TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_count);
		tvCount.setText(Language.BEITOU + Config.MAOHAO + Integer.toString(beishu));
		// 消费
		TextView tvCost = (TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_cost);
		tvCost.setText(Language.MSG_RESULT_HEMAI_XIAOFEI + Language.POPLOTTERYTIP_RMB + f.format((long) zhushu * (long) beishu * 2));
		// 余额
		TextView tvBalance = (TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_balance);
		tvBalance.setText(Language.MSG_RESULT_HEMAI_SHENGYUJINE + Language.POPLOTTERYTIP_RMB + UserInfo.getUserInfo().getUserBalance());
		dialogBuySucceed.show();
		OnClickListener dialogSucceedListener = new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				BeidanLotteryInfo.getBeiDanLotteryInfo().getLotterList().clear();

				switch (v.getId()) {
				// 继续购彩
				case R.id.viewerutildialogshowbuysucceed_btn_back:
					dialogBuySucceed.dismiss();
					Message msg = BeidanChooseList.handlerChoosePage.obtainMessage(BeidanChooseList.DO_REFRESH, null);
					BeidanChooseList.handlerChoosePage.sendMessage(msg);
					((Activity) context).setContentView(chooseListPage.getView());
					break;
				// 返回购彩大厅
				case R.id.viewerutildialogshowbuysucceed_btn_toHall:
					dialogBuySucceed.dismiss();
					BeidanChooseAct.pageFlag = 0;
					((Activity) context).getParent().finish();
					break;

				default:
					break;
				}

			}
		};
		Button btnBack = (Button) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_btn_back);
		btnBack.setOnClickListener(dialogSucceedListener);
		Button btnToHall = (Button) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_btn_toHall);
		btnToHall.setOnClickListener(dialogSucceedListener);
		dialogBuySucceed.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss(DialogInterface dialog) {
			}
		});

	}

	private Dialog dialogBalanceNotEnough;
	private View.OnClickListener moneyNotEnoughListener;

	/** 余额不足提示 */
	private void popBalanceNotEnoughAlert() {
		if (dialogBalanceNotEnough == null) {
			dialogBalanceNotEnough = new Dialog(context, R.style.dialog);
		}

		if (moneyNotEnoughListener == null) {
			moneyNotEnoughListener = new OnClickListener() {

				@Override
				public void onClick(View v) {
					switch (v.getId()) {
					case R.id.shiyixuanwu_alert_money_not_enough_btn_cancel:
						break;

					case R.id.shiyixuanwu_alert_money_not_enough_btn_recharge:// 跳转到充值界面
						UserInfo.getUserInfo().setBuyingRecharge(true);
						((Activity) context).startActivity(new Intent().setClass(context, RechargeMenuPage.class));
						break;
					}
					dialogBalanceNotEnough.dismiss();
				}
			};
		}

		View view = ((Activity) context).getLayoutInflater().inflate(R.layout.shiyixuanwu_alert_money_not_enough, null);
		Button btnCancel = (Button) view.findViewById(R.id.shiyixuanwu_alert_money_not_enough_btn_cancel);
		btnCancel.setOnClickListener(moneyNotEnoughListener);
		Button btnRecharge = (Button) view.findViewById(R.id.shiyixuanwu_alert_money_not_enough_btn_recharge);
		btnRecharge.setOnClickListener(moneyNotEnoughListener);
		dialogBalanceNotEnough.setContentView(view);
		dialogBalanceNotEnough.show();
	}

	@Override
	public void setSportteryChooseListPage(SportteryChooseList chooseListPage) {
		this.chooseListPage = chooseListPage;
	}

}
